package com.zxl.always.on.display.ui.helper;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.bean.ClockInfo;
import com.zxl.always.on.display.track.MobclickAgentHelper;
import com.zxl.always.on.display.track.StatisticsConstant;
import com.zxl.always.on.display.track.StatisticsType;
import com.zxl.always.on.display.ui.adapter.ColorPaletteAdapter;
import com.zxl.always.on.display.ui.widget.BackgroundSelectView;
import com.zxl.always.on.display.ui.widget.ClockSelectView;
import com.zxl.always.on.display.ui.widget.TabGroupView;
import com.zxl.always.on.display.ui.widget.clock.BaseClockView;

/**
 * Created by Administrator on 2018/3/6.
 */

public class TabViewHelper implements TabGroupView.OnItemClickListener {

    private Activity mActivity;

    private GridView mPaletteView;

    private TabGroupView mTabGroupView;

    private ColorPaletteAdapter mColorPaletteAdapter;

    private ClockSelectView mClockSelectView;

    private BackgroundSelectView mBackgroundSelectView;

    public TabViewHelper(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public void initView(final BaseClockView mBaseClockView, ClockStyle clockStyle) {
        ClockInfo clockInfo = new ClockInfo(clockStyle);
        initView(mBaseClockView, clockInfo);
        mBaseClockView.setDigitalStyle(clockInfo.clockPosition);
        mBaseClockView.setBackgroundStyle(clockInfo.backgroundPosition);
        mBaseClockView.setPaletteColor(clockInfo.palettePosition);
        mBaseClockView.refreshTime();
    }

    private void initView(final BaseClockView mBaseClockView, final ClockInfo clockInfo) {
        mTabGroupView = (TabGroupView) findViewById(R.id.tab_view);
        mTabGroupView.setOnItemClickListener(this);

        mPaletteView = (GridView) findViewById(R.id.palette_grid_view);
        mPaletteView.setDrawingCacheEnabled(false);
        mPaletteView.setAdapter(mColorPaletteAdapter = new ColorPaletteAdapter(mActivity, clockInfo.palettePosition));
        mPaletteView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mColorPaletteAdapter.setSelectPosition(position);
                mBaseClockView.setPaletteColor(position);
                MobclickAgentHelper.onEvent(mActivity, StatisticsType.SETTINGS, StatisticsConstant.Event.CLOCK_PALETTE, clockInfo.clockStyle + "_" + position);
            }
        });

        mClockSelectView = (ClockSelectView) findViewById(R.id.clock_style);
        mClockSelectView.setData(clockInfo.clockPosition, clockInfo.clockStyle);
        mClockSelectView.setOnItemClickListener(new TabGroupView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mColorPaletteAdapter.setSelectPosition(0);
                mClockSelectView.setSelectPosition(position);
                mBaseClockView.setDigitalStyle(position);
                mBaseClockView.refreshTime();
                MobclickAgentHelper.onEvent(mActivity, StatisticsType.SETTINGS, StatisticsConstant.Event.CLOCK_STYLE, clockInfo.clockStyle + "_" + position);
            }
        });

        mBackgroundSelectView = (BackgroundSelectView) findViewById(R.id.background_style);
        mBackgroundSelectView.setSelectPosition(clockInfo.backgroundPosition);
        mBackgroundSelectView.setOnItemClickListener(new TabGroupView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mBaseClockView.setBackgroundStyle(position);
                MobclickAgentHelper.onEvent(mActivity, StatisticsType.SETTINGS, StatisticsConstant.Event.CLOCK_BG, clockInfo.clockStyle + "_" + position);
            }
        });
    }

    @Override
    public void onItemClick(int position) {
        switch (position) {
            case 0:
                if (mClockSelectView.getVisibility() == View.GONE) {
                    mClockSelectView.setVisibility(View.VISIBLE);
                }
                mPaletteView.setVisibility(View.GONE);
                mBackgroundSelectView.setVisibility(View.GONE);
                break;
            case 1:
                if (mPaletteView.getVisibility() == View.GONE) {
                    mPaletteView.setVisibility(View.VISIBLE);
                }
                mClockSelectView.setVisibility(View.GONE);
                mBackgroundSelectView.setVisibility(View.GONE);
                break;
            case 2:
                if (mBackgroundSelectView.getVisibility() == View.GONE) {
                    mBackgroundSelectView.setVisibility(View.VISIBLE);
                }
                mClockSelectView.setVisibility(View.GONE);
                mPaletteView.setVisibility(View.GONE);
                break;
        }
    }

    private View findViewById(int id) {
        return mActivity.findViewById(id);
    }

    public void hideTabContent() {
        mPaletteView.setVisibility(View.GONE);
        mClockSelectView.setVisibility(View.GONE);
        mBackgroundSelectView.setVisibility(View.GONE);
        mTabGroupView.setCheckedPosition(-1);
    }

    public void saveData() {
        ClockStyle clockStyle = mClockSelectView.getClockStyle();
        int palettePosition = mColorPaletteAdapter.getSelectPosition();
        int backgroundPosition = mBackgroundSelectView.getSelectPosition();
        int clockPosition = mClockSelectView.getSelectPosition();
        ClockInfo clockInfo = new ClockInfo(clockStyle, palettePosition, backgroundPosition, clockPosition);
        clockInfo.saveData();
    }
}
